package com.exam.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.exam.bean.PaperSourceType;
import com.exam.service.PaperSourceTypeService;
import com.exam.service.impl.PaperSourceTypeServiceImpl;
import com.exam.util.Page;
import com.exam.util.PageResult;
import com.opensymphony.xwork2.ActionSupport;

public class SelectSourceTypeAction extends ActionSupport {
 
	private static final long serialVersionUID = 1L;
	
	private PaperSourceType paperSourceType = new PaperSourceType();
	private List<PaperSourceType> pstList = new ArrayList<PaperSourceType>();
	private Page page = new Page();

	// 为分页处理服务的方法
	@SuppressWarnings("unchecked")
	public void listForSourceTypePageService(PaperSourceTypeService pcts,
			String strWhere) throws Exception { 
		PageResult pageResult = null;
		getPage().setEveryPage(10);
		if (strWhere != null && !"".equals(strWhere)) {
			pageResult = pcts.findListByPage(strWhere, getPage());// 按条件查询
		} else {
			pageResult = pcts.findListByPage(getPage());// 简单分页
		}
		setPage(pageResult.getPage());// 获得分页信息
		setPstList(pageResult.getList());// 获得记录
	}

	//按条件进行分页查询
	public String selectByWherePage(){
		PaperSourceTypeService service = new PaperSourceTypeServiceImpl();
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String nameStr = request.getParameter("name"); 
		try {
			if(nameStr!=null&&!"".equals(nameStr)){
				if(page.getCurrentPage()>1){//存在条件的时候，将分页信息设置为初始态
					page = new Page();
				}
				listForSourceTypePageService(service, "name='"+nameStr+"'");
			}else{
				listForSourceTypePageService(service, "");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "exception";
		}
		return "selectByPage";
	}
	
	//使用按分页查询
	public String listByPage() {
		PaperSourceTypeService service = new PaperSourceTypeServiceImpl();
		try {
			listForSourceTypePageService(service, "");
		} catch (Exception e) {
			e.printStackTrace();
			return "exception";
		}
		return "selectByPage";
	}

	public String select(){
		return selectByWherePage();
	}
	
	public PaperSourceType getPaperSourceType() {
		return paperSourceType;
	}

	public void setPaperSourceType(PaperSourceType paperSourceType) {
		this.paperSourceType = paperSourceType;
	}

	public List<PaperSourceType> getPstList() {
		return pstList;
	}

	public void setPstList(List<PaperSourceType> pstList) {
		this.pstList = pstList;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	
}
